package com.ghh.dao;

import com.ghh.pojo.CheckGroup;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CheckGroupDao {

    //添加检查组，返回id
    @Insert("insert into t_checkgroup values (#{id},#{code},#{name},#{helpCode},#{sex},#{remark},#{attention})")
    @SelectKey(statement = "select last_insert_id()",before = false,keyProperty = "id",resultType = Integer.class,keyColumn = "id")
    void addCheckgroup(CheckGroup checkGroup);

    //将新增的检查组id与检查项id添加到关联表中
    @Insert("insert into t_checkgroup_checkitem values (#{checkgroup_Id},#{checkitem_Id})")
    void addCheckGroupAndCheckitem(@Param("checkgroup_Id") Integer checkgroup_Id,@Param("checkitem_Id") Integer checkitem_Id);

    //分页查询检查组
    @Select("<script>select * from t_checkgroup <if test=\"value!=null and value!=''\">where code like '%${value}%' or name like '%${value}%' or helpCode like '%${value}%'</if></script>")
    Page<CheckGroup> findByCondition(@Param("value") String queryString);

    //根据id查询检查组
    @Select("select * from t_checkgroup where id = #{id}")
    CheckGroup findById(Integer id);

    //根据检查组的id查询检查项的id
    @Select("select checkitem_Id from t_checkgroup_checkitem where checkgroup_Id = #{id}")
    List<Integer> findCheckItemIdsByCheckGroupId(Integer id);

    //修改检查组
    @Update("update t_checkgroup set code=#{code},name=#{name},helpCode=#{helpCode},sex=#{sex},remark=#{remark},attention=#{attention} where id = #{id}")
    void updateCheckGroup(CheckGroup checkGroup);

    //根据检查组id删除关联表
    @Delete("delete from t_checkgroup_checkitem where checkgroup_id = #{id}")
    void deleteCheckGroupAndCheckitem(Integer id);

    //删除检查组
    @Delete("delete from t_checkgroup where id = #{id}")
    void deleteCheckGroup(Integer id);

    //查询所有
    @Select("select * from t_checkgroup")
    List<CheckGroup> findAll();

    @Select("select * from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id} )")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "checkItems", column = "id",
                    many = @Many(select = "com.ghh.dao.CheckitemDao.findAllCheckItem"))
    })
    List<CheckGroup> findAllCheckGroup(Integer id);
}
